#include"proc.h"

#include<stdio.h>
#include<errno.h>

int get_kernel_version( int * major, int * minor, int * patch, int * err )
{
  FILE * fp = fopen( "/proc/sys/kernel/osrelease", "r" );
  if( fp == NULL )
    {
      *err = errno;
      return -1;
    }
  // something like 2.6.24-16-generic
  if( fscanf( fp, "%d.%d.%d", major, minor, patch ) != 3 )
    {
      *err = 0;
      return -2;
    }
  fclose( fp );
  return 0;
}

int get_cpu_number( int * err )
{
  char * linebuf = NULL;
  size_t linebufsize = 0;
  
  FILE * fp = fopen( "/proc/stat", "r" );
  if( fp == NULL )
    {
      *err = errno;
      return -1;
    }
  int r = 0;
  while( getline( &linebuf, &linebufsize, fp ) != -1 )
    {
      if( strncmp( linebuf, "cpu", 3 ) == 0 )
        {
          ++r;
        }
      else
        {
          break;
        }
    }
  fclose( fp );
  return r - 1;
}
